'''
Created on 25/03/2009

@author: eh01
'''
from yarra.datastore import dsconstants, dsofactory
from yarra.datastore.dsstub import DSStub
from yarra.event import eventconstants
from yarra.event.event import Event
from yarra.event.eventhandler import BaseEventHandler
from yarra.event.eventmanager import EventManager
import httplib
import os


class HTTPRequestGETStaticPageHandler(BaseEventHandler):
    '''
    Handles all static HTML GET requests
    '''


    def __init__(self):
        '''
        Constructor
        '''
    
    def do(self, data):
        dso = data
        http_request = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_HTTP_REQUEST).get_literal()
        lock = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_LOCK).get_literal()                    
        relative_path = None
        event_type = eventconstants.HTTP_RESPONSE_EVENT_TYPE
        # get the path of the appropriate html template
        ds_stub = DSStub()
        if ds_stub.is_hub():
            relative_path = "../../../pages/hub" + http_request.path
        else:
            relative_path = "../../../pages/im" + http_request.path
            
        # get the html
        template_path = os.path.join(os.path.dirname(__file__), relative_path)
        file = open(template_path)
        html_template_string = file.read()
        
        # create new dso        
        new_dso = dsofactory.create_dso_for_im_http_response_handler(httplib.OK, 
                                                               'text/html', 
                                                               html_template_string, 
                                                               http_request, 
                                                               lock)            
        event = Event(event_type, new_dso)
        EventManager().add_event(event)